{% extends "sysadmin/base.html" %}
{% load seahub_tags i18n %}

{% block cur_settings %}tab-cur{% endblock %}

{% block right_panel %}
<h3 class="hd">{% trans "Settings" %}</h3>

<div class="seahub-web-settings">

    <p class="tip">{% trans "Note: Settings via web interface are saved in database table (seahub-db/constance_config). They have a higher priority over the settings in config files." %}</p>

    <h4>URL</h4>

    {% with type="input" setting_display_name="SERVICE_URL" setting_name="SERVICE_URL" setting_val=config_dict.SERVICE_URL %}
        {% trans "The URL of the server, like https://seafile.example.com or http://192.168.1.2:8000" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_display_name="FILE_SERVER_ROOT" setting_name="FILE_SERVER_ROOT" setting_val=config_dict.FILE_SERVER_ROOT %}
        {% trans "The internal URL for downloading/uploading files. Users will not be able to download/upload files if this is not set correctly. If you config Seafile behind Nginx/Apache, it should be SERVICE_URL/seafhttp, like https://seafile.example.com/seafhttp ." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}
  
    <h4>{% trans "Branding" %}</h4>

    {% with type="input" setting_display_name="SITE_TITLE" setting_name="SITE_TITLE" setting_val=config_dict.SITE_TITLE %}
        {% trans "Site title shown in a browser tab" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_display_name="SITE_NAME" setting_name="SITE_NAME" setting_val=config_dict.SITE_NAME %}
        {% trans "Site name used in email sending" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="file" setting_display_name="Logo" setting_name="logo" file_path=logo_path file_width=256 file_height=64 help_tip="logo.png, 256px * 64px" %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="file" setting_display_name="Favicon" setting_name="favicon" file_path=favicon_path file_width=32 file_height=32 help_tip="favicon.ico, 32px * 32px" %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="file" setting_name="login_bg_image" file_path=login_bg_image_path file_width=240 file_height=160 help_tip="login-bg.jpg, 2400px * 1600px" %}
        {% trans "Login Background Image" as setting_display_name %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_name="ENABLE_BRANDING_CSS" setting_val=config_dict.ENABLE_BRANDING_CSS %}
        {% trans "ENABLE_BRANDING_CSS" as setting_display_name %}
        {% trans "Use custom CSS" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="code-textarea" setting_name="CUSTOM_CSS" setting_val=config_dict.CUSTOM_CSS %}
        {% trans "Custom CSS" as setting_display_name %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    <h4>{% trans "User" %}</h4>

    {% with type="checkbox" setting_name="ENABLE_SIGNUP" setting_val=config_dict.ENABLE_SIGNUP %}
        {% trans "allow new registrations" as setting_display_name %}
        {% trans "Allow new user registrations. Uncheck this to prevent anyone from creating a new account." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_name="ACTIVATE_AFTER_REGISTRATION" setting_val=config_dict.ACTIVATE_AFTER_REGISTRATION %}
        {% trans "activate after registration" as setting_display_name %}
        {% trans "Activate user immediately after registration. If unchecked, a user need to be activated by administrator or via activation email" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_name="REGISTRATION_SEND_MAIL" setting_val=config_dict.REGISTRATION_SEND_MAIL %}
        {% trans "send activation email" as setting_display_name %}
        {% trans "Send activation Email after user registration." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_name="LOGIN_REMEMBER_DAYS" setting_val=config_dict.LOGIN_REMEMBER_DAYS %}
        {% trans "keep sign in" as setting_display_name %}
        {% trans "Number of days that keep user sign in." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_display_name="LOGIN_ATTEMPT_LIMIT" setting_name="LOGIN_ATTEMPT_LIMIT" setting_val=config_dict.LOGIN_ATTEMPT_LIMIT %}
        {% trans "The maximum number of failed login attempts before showing CAPTCHA." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_display_name="FREEZE_USER_ON_LOGIN_FAILED" setting_name="FREEZE_USER_ON_LOGIN_FAILED" setting_val=config_dict.FREEZE_USER_ON_LOGIN_FAILED %}
        {% trans "Freeze user account when failed login attempts exceed limit." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}


    <h4>{% trans "Groups" %}</h4>

    {% with type="checkbox" setting_display_name="ENABLE_SHARE_TO_ALL_GROUPS" setting_name="ENABLE_SHARE_TO_ALL_GROUPS" setting_val=config_dict.ENABLE_SHARE_TO_ALL_GROUPS %}
        {% trans "Enable users to share libraries to any groups in the system." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}


    <h4>{% trans "Password" %}</h4>

    {% with type="checkbox" setting_name="USER_STRONG_PASSWORD_REQUIRED" setting_val=config_dict.USER_STRONG_PASSWORD_REQUIRED %}
        {% trans "strong password" as setting_display_name %}
        {% trans "Force user to use a strong password when sign up or change password." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_name="FORCE_PASSWORD_CHANGE" setting_val=config_dict.FORCE_PASSWORD_CHANGE %}
        {% trans "force password change" as setting_display_name %}
        {% trans "Force user to change password when account is newly added or reset by admin" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_name="USER_PASSWORD_MIN_LENGTH" setting_val=config_dict.USER_PASSWORD_MIN_LENGTH %}
        {% trans "password minimum length" as setting_display_name %}
        {% trans "The least number of characters an account password should include." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_name="USER_PASSWORD_STRENGTH_LEVEL" setting_val=config_dict.USER_PASSWORD_STRENGTH_LEVEL %}
        {% trans "password strength level" as setting_display_name %}
        {% trans "The level(1-4) of an account password's strength. For example, '3' means password must have at least 3 of the following: num, upper letter, lower letter and other symbols" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_name="ENABLE_TWO_FACTOR_AUTH" setting_val=config_dict.ENABLE_TWO_FACTOR_AUTH %}
        {% trans "enable two factor authentication" as setting_display_name %}
        {% trans "Enable two factor authentication" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    <h4>{% trans "Library" %}</h4>

    {% with type="checkbox" setting_name="ENABLE_REPO_HISTORY_SETTING" setting_val=config_dict.ENABLE_REPO_HISTORY_SETTING %}
        {% trans "library history" as setting_display_name %}
        {% trans "Allow user to change library history settings" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_name="ENABLE_ENCRYPTED_LIBRARY" setting_val=config_dict.ENABLE_ENCRYPTED_LIBRARY %}
        {% trans "encrypted library" as setting_display_name %}
        {% trans "Allow user to create encrypted libraries" as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_name="REPO_PASSWORD_MIN_LENGTH" setting_val=config_dict.REPO_PASSWORD_MIN_LENGTH %}
        {% trans "library password minimum length" as setting_display_name %}
        {% trans "The least number of characters an encrypted library password should include." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="input" setting_name="SHARE_LINK_PASSWORD_MIN_LENGTH" setting_val=config_dict.SHARE_LINK_PASSWORD_MIN_LENGTH %}
        {% trans "download/upload link password minimum length" as setting_display_name %}
        {% trans "The least number of characters a download/upload link password should include." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    {% with type="checkbox" setting_display_name="ENABLE_USER_CREATE_ORG_REPO" setting_name="ENABLE_USER_CREATE_ORG_REPO" setting_val=config_dict.ENABLE_USER_CREATE_ORG_REPO %}
        {% trans "Allow user to add organization libraries. Otherwise, only system admin can add organization libraries." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    <h4>{% trans "Online Preview" %}</h4>

    {% with type="textarea" setting_name="TEXT_PREVIEW_EXT" setting_val=config_dict.TEXT_PREVIEW_EXT %}
        {% trans "text file extensions" as setting_display_name %}
        {% trans "Extensions of text files that can be online previewed, each suffix is separated by a comma." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}

    <h4>{% trans "Sync" %}</h4>

    {% with type="checkbox" setting_display_name="DISABLE_SYNC_WITH_ANY_FOLDER" setting_name="DISABLE_SYNC_WITH_ANY_FOLDER" setting_val=config_dict.DISABLE_SYNC_WITH_ANY_FOLDER %}
        {% trans "If turn on, the desktop clients will not be able to sync a folder outside the default Seafile folder." as help_tip %}
        {% include "snippets/web_settings_form.html" %}
    {% endwith %}
</div>
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('.input, .textarea', $('.web-setting-form')).on('focus', function() {
    var $otherFormSubmit = $('.web-setting-form .submit:visible');
    if ($otherFormSubmit.length > 0) {
        $otherFormSubmit.siblings('.cancel').trigger('click');
    }
    var $form = $(this).closest('form');
    $('.submit, .cancel', $form).show();
});

$('.web-setting-form .cancel').on('click', function() {
    var $form = $(this).closest('form');
    $('.error, .submit, .cancel', $form).hide();
    var $input = $('.input, .textarea', $form);
    $input.val($input.attr('data-cur'));
});

$(document).on('click', function(e) {
    var target =  e.target || event.srcElement;
    var $op = $('.web-setting-form .submit:visible');
    if ($op.length && !$('.input, .textarea, .submit, .cancel', $op.closest('form')).is(target)) {
        $('.cancel', $op.closest('form')).trigger('click');
    }
});

$('.web-setting-checkbox').on('change', function() {
    var checkbox = $(this),
        value,
        key = checkbox.attr('name');

    if (checkbox.prop('checked')) {
        value = 1;
    } else {
        value = 0;
    }

    $.ajax({
        url: "{% url 'sys_settings' %}",
        type: 'POST',
        dataType: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: { 'key': key, 'value': value },
        success: function() {
            feedback("{% trans "Success" %}", 'success');
        },
        error: ajaxErrorHandler
    });
});

$('.web-input-setting-form, .web-textarea-setting-form').on('submit', function() {
    var $form = $(this),
        $error = $form.find('.error'),
        $input = $form.hasClass('web-input-setting-form') ? $('.input', $form) : $('.textarea', $form),
        $sb_btn = $('.submit', $form),
        $cancel_btn = $('.cancel', $form),
        key = $input.attr('name'),
        value = $.trim($input.val());

    if (!value) {
        $error.html("{% trans "It is required." %}").show();
        return false;
    }

    if (value == $input.attr('data-cur')) { // no change
        $cancel_btn.trigger('click');
        return false;
    }

    disable($sb_btn);
    $.ajax({
        url: "{% url 'sys_settings' %}",
        type: 'POST',
        dataType: 'json',
        beforeSend: prepareCSRFToken,
        data: { 'key': key, 'value': value },
        success: function() {
            $input.val(value).attr('data-cur', value);
            $error.html('').hide();
            enable($sb_btn);
            $sb_btn.hide();
            $cancel_btn.hide();
            feedback("{% trans "Success" %}", 'success');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.responseText) {
                $error.html(JSON.parse(jqXHR.responseText).error).show();
            } else {
                $error.html("{% trans "Failed. Please check the network." %}").show();
            }
            enable($sb_btn);
        }
    });
    return false;
});


$('.web-setting-file-upload-btn').on('click', function() {
    var $container = $(this).closest('.web-setting-file-upload');
    $('.web-setting-file-upload-input', $container).trigger('click');
});

$('.web-setting-file-upload-input').on('change', function() {
    var $input = $(this);
    var file;
    if ($input[0].files) {
        file = $input[0].files[0];
    } else {
        return;
    }

    var input_name = $input.attr('name');
    var fd = new FormData();
    var url;

    switch(input_name) {
        case 'logo': url = '{% url 'api-v2.1-admin-logo' %}'; break;
        case 'favicon': url = '{% url 'api-v2.1-admin-favicon' %}'; break;
        case 'login_bg_image': url = '{% url 'api-v2.1-admin-login-background-image' %}'; break;
    }

    fd.append(input_name, file);
    $.ajax({
        url: url,
        type: 'POST',
        data: fd,
        processData: false,
        contentType: false,
        beforeSend: prepareCSRFToken,
        success: function(){
            location.reload(true);
        },
        error: ajaxErrorHandler
    });
});
</script>
{% endblock %}
